package gui;

import java.io.Serializable;

import javax.swing.JOptionPane;

import building.Cell;

/**
 * This class implement a Floor built of a Cell matrix where the agent acts.
 * 
 * @see building.Cell
 * 
 */
public class Map implements Serializable {

	private static final long serialVersionUID = 1L;

	public int length;

	public int width;

	public int nfloor;

	public Cell[][][] map;

	public Map() {
		map = new Cell[10][10][10];
	}

	public Map(int l, int w, int nFloor) {

		this.length = l;
		this.width = w;
		this.nfloor = nFloor;

		map = new Cell[length][width][nfloor];

	}

	/**
	 * Genera una mappa
	 * 
	 * @param l
	 * @param w
	 * @param nFloor
	 */
	public void genMap(int l, int w, int nFloor) {

		this.length = l;
		this.width = w;
		this.nfloor = nFloor;
		int row = (int) (Math.random() % length);
		int col = (int) (Math.random() % width);
		int flo = (int) (Math.random() % nfloor);

		if (l < 3 || w < 3) {
			JOptionPane.showMessageDialog(null, "Minimun allowed size is " + 4,
					"Warning", JOptionPane.WARNING_MESSAGE);

		}

		for (int f = 0; f < nfloor; f++) {
			for (int x = 0; x < length; x++) {
				for (int y = 0; y < width; y++) {
					Cell c = null;

					if (f == 0 && x == 0 && y == 0) {
						c = new Cell(Cell.DOOR);

					}
					if (f == 1 && x == 0 && y == 0) {
						c = new Cell(Cell.STAIRS);

					}

					if (f == 1 && x == 0 && y == 1) {
						c = new Cell(Cell.DOOR);

					}

					if (x == row && y == col && flo == f) {
						c = new Cell(Cell.FIRE);
					}

					c = new Cell(Cell.FREE);
					this.setCell(x, y, f, c);
				}
			}
		}

	}

	public Cell getCell(int x, int y, int f) {
		return map[x][y][f];
	}

	public void setCell(int x, int y, int f, Cell c) {
		this.map[x][y][f] = c;
	}

	public Cell[][][] getMap() {
		return this.map;
	}

	public void cleanCell() {

		for (int f = 0; f < nfloor; f++) {
			for (int x = 0; x < length; x++) {
				for (int y = 0; y < width; y++) {
					Cell c = new Cell(Cell.FREE);
					this.setCell(x, y, f, c);
				}
			}
		}

	}

	/*
	 * Generates randomly a floor scenario
	 * 
	 * @param numDirtySquares quantity of dirty squares
	 * 
	 * @param numObstacles quantity of obstacles
	 *//*
		 * public Coord generateObject(int width){ this.length = 5;
		 * 
		 * 
		 * //Order the rooms in random way
		 * 
		 * LinkedList <roomType> rooms_r= new LinkedList<roomType>();
		 * 
		 * rooms_r.add(roomType.BEDROOM); rooms_r.add(roomType.LIVINGROOM);
		 * rooms_r.add(roomType.HALL); rooms_r.add(roomType.KITCHEN);
		 * 
		 * Collections.shuffle(rooms_r);
		 * 
		 * //initialization of the rooms for(int i=0; i<this.room.length-1;
		 * i++){ this.room[i] = rooms_r.get(i);
		 * System.out.println(this.room[i]); }
		 * 
		 * this.room[length-1] = roomType.ENTRANCE;
		 * 
		 * LinkedList <Square.Type> rooms_objects= new
		 * LinkedList<Square.Type>();
		 * 
		 * for (int i = 0; i < length; i++){
		 * 
		 * if(this.room[i]== roomType.BEDROOM){
		 * rooms_objects.add(Square.Type.SMOKING);
		 * rooms_objects.add(Square.Type.DOOR); for (int j = 2; j < width; j++){
		 * rooms_objects.add(Square.Type.CLEAN);
		 * 
		 * } Collections.shuffle(rooms_objects); for(int k=0; k<width; k++)
		 * floor[i][k].type= rooms_objects.get(k); rooms_objects.clear(); }
		 * if(this.room[i]== roomType.HALL){
		 * rooms_objects.add(Square.Type.FLOWERS);
		 * rooms_objects.add(Square.Type.DOOR); for (int j = 2; j < width; j++){
		 * rooms_objects.add(Square.Type.CLEAN);
		 * 
		 * } Collections.shuffle(rooms_objects); for(int k=0; k<width; k++)
		 * floor[i][k].type= rooms_objects.get(k); rooms_objects.clear(); }
		 * if(this.room[i]== roomType.LIVINGROOM){
		 * rooms_objects.add(Square.Type.TABLE);
		 * rooms_objects.add(Square.Type.DOOR); for (int j = 2; j < width; j++){
		 * rooms_objects.add(Square.Type.CLEAN);
		 * 
		 * } Collections.shuffle(rooms_objects); for(int k=0; k<width; k++)
		 * floor[i][k].type= rooms_objects.get(k); rooms_objects.clear(); }
		 * if(this.room[i]== roomType.KITCHEN){
		 * rooms_objects.add(Square.Type.MEAL);
		 * rooms_objects.add(Square.Type.DOOR); for (int j = 2; j < width; j++){
		 * rooms_objects.add(Square.Type.CLEAN);
		 * 
		 * } Collections.shuffle(rooms_objects); for(int k=0; k<width; k++)
		 * floor[i][k].type= rooms_objects.get(k); rooms_objects.clear(); }
		 * 
		 * }
		 * 
		 * 
		 * 
		 * 
		 * 
		 * //assign ciccio in a random position while(true){ Random randomGen =
		 * new Random(); int col = Math.abs(randomGen.nextInt()) % this.width;
		 * System.out.println("col" + col);
		 * 
		 * int row = Math.abs(randomGen.nextInt()) % this.length;
		 * System.out.println("row" + row);
		 * 
		 * if(this.floor[row][col].type== Square.Type.CLEAN){
		 * 
		 * return new Coord(row, col); } }
		 * 
		 * 
		 * 
		 * LinkedList<Cell> cells = new LinkedList<Cell>();
		 * 
		 * for (int i = 0; i < length; i++) for (int j = 0; j < width; j++)
		 * if(floor[i][j].type == Square.Type.CLEAN) cells.add(new Cell(i, j));
		 * 
		 * Collections.shuffle(cells); Random randomGen = new Random(); int size
		 * = cells.size();
		 * 
		 * 
		 * }
		 *//**
	 * 
	 * @return the current number of clean squares.
	 */
	/*
	 * public int squaresNowCleaned(){ int cleanedSquare = 0; for (int i = 0; i
	 * < length; i++) for (int j = 0; j < width; j++) if(get(i,j) ==
	 * Square.Type.CLEAN) cleanedSquare++; return cleanedSquare; }
	 * 
	 * public void clear(){ for (int i = 0; i < length; i++) for (int j = 0; j <
	 * width; j++){ if(i==length-1){ floor[i][j].type = Square.Type.GARDEN; }
	 * else floor[i][j].type = Square.Type.CLEAN;
	 * 
	 * } }
	 * 
	 * public void load(Floor floor){ this.floor = floor.floor; this.width =
	 * floor.width; this.length = floor.length; this.room = floor.room; }
	 *//**
	 * Confirm if there is an obstacle in the position
	 * 
	 * @param i
	 *            the row position of the square
	 * @param j
	 *            the column position of the square
	 * 
	 * @return TRUE if there is an obstacle.
	 */
	/*
	 * public boolean obstacle(int i, int j) { return get(i,j) ==
	 * Square.Type.DOOR; }
	 *//**
	 * 
	 * @param i
	 *            the row position of the square
	 * @param j
	 *            the column position of the square
	 * @return the type of the square
	 */
	/*
	 * public Square.Type get(int i, int j){ if(i<0 || j<0 || i>=length ||
	 * j>=width) return Square.Type.STATE; return this.floor[i][j].type; }
	 *//**
	 * Set the state of a square in the position (i,j).
	 * 
	 * @param i
	 *            the square's row
	 * @param j
	 *            the square's column
	 * @param st
	 *            the square's state
	 */
	/*
	 * public void set(int i, int j, Square.Type st){ if(i<0 || j<0 || i>=length
	 * || j>=width) return; this.floor[i][j].type = st; }
	 * 
	 * Search object type and return position X Y* public Coord
	 * search(Square.Type obj) { int i = 0; int j = 0; for (i = 0; i <
	 * this.length; i++) for (j = 0; j < this.width; j++) { if
	 * (this.floor[i][j].type.equals(obj)){
	 * 
	 * // System.out.println("L'oggetto : "+ obj.toString() + //
	 * " si trova in room " + i + " pos " + j ); return new Coord(i, j); }
	 * 
	 * } System.out.println("L'oggetto : "+ obj.toString() +
	 * " non � stato trovato." );
	 * 
	 * return new Coord(-1, -1); }
	 */

}
